﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DiagramCreator.Nodes;
using DiagramCreator.Arrows;
using System.Windows.Resources;
using DiagramCreator.Diagram;

namespace DiagramCreator
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        #region - Konstruktor -

        public Window1()
        {
            InitializeComponent();
            this.Focusable = true;
            this.Background = Brushes.Gray;
            this.KeyDown += new KeyEventHandler(Window1_KeyDown);
            this.KeyUp += new KeyEventHandler(Window1_KeyUp);
            this.SizeChanged += new SizeChangedEventHandler(Window1_SizeChanged);
            
            // Nodes Align Split Button
            WrapPanel wp = new WrapPanel();
            Image alignCenterImg = new Image();   
            
            // TODO - dodać tworzenie nowej zakładki za pomocą funkcji z kompletem operacji wiążących eventy z diagramu
            //
            //CurrentSheet.DiagramTitleChanged += new EventHandler<DiagramCreator.Utils.Events.ObjectEventArgs<string>>(CurrentSheet_DiagramTitleChanged);
            TabManager.Items.CurrentChanged += new EventHandler(TabManager_Items_CurrentChanged);            
        }

        void TabManager_Items_CurrentChanged(object sender, EventArgs e)
        {
            //Title = CurrentSheet.Title;
        }

        void CurrentSheet_DiagramTitleChanged(object sender, DiagramCreator.Utils.Events.ObjectEventArgs<string> e)
        {
            if (CurrentTabItem != null)
            {
                (CurrentTabItem.Header as StackPanel).Children.OfType<Label>().First().Content
                    = e.Argument;
            }
        }

        private TabItem CurrentTabItem
        {
            get { return TabManager.Items.CurrentItem as TabItem; }
        }        

        void Window1_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (CurrentSheet != null)
            {
                if (CurrentSheet.AutoSizing)
                    CurrentSheet.AdjustSizeToChildrenControls();
            }
        }

        #endregion

        #region - GetBitmapImage -

        private BitmapSource GetBitmapImage(string path)
        {
            StreamResourceInfo sri = Application.GetResourceStream(new Uri(path));
            BitmapImage bmp = new BitmapImage();
            bmp.BeginInit();
            bmp.StreamSource = sri.Stream;
            bmp.EndInit();
            return bmp;
        }

        #endregion

        #region - Window1_KeyUp -

        void Window1_KeyUp(object sender, KeyEventArgs e)
        {
            CurrentSheet.KeyUpHandler(e);
        } 

        #endregion

        #region - Window1_KeyDown- 

        void Window1_KeyDown(object sender, KeyEventArgs e)
        {
            CurrentSheet.KeyDownHandler(e);
        }

        #endregion

        public DiagramSheet CurrentSheet
        {
            get
            {
                return (Diagram.DiagramSheet)((ScrollViewer)this.TabManager.SelectedContent).Content;
            }
        }

        private void BtnSave_MouseDown(object sender, RoutedEventArgs e)
        {
            CurrentSheet.SaveToFile();
            e.Handled = true;
        }

        private void BtnOpen_MouseDown(object sender, RoutedEventArgs e)
        {
            CurrentSheet.LoadFromFile();
            e.Handled = true;
        }

        private void BtnToLeft_MouseDown(object sender, RoutedEventArgs e)
        {
            CurrentSheet.AlignSelectedNodesToLeft();
            e.Handled = true;
        }

        private void BtnToRight_MouseDown(object sender, RoutedEventArgs e)
        {
            CurrentSheet.AlignSelectedNodesToRight();
            e.Handled = true;
        }

        private void BtnDistributeVertical_MouseDown(object sender, RoutedEventArgs e)
        {
            CurrentSheet.DistributeVertically();
            e.Handled = true;
        }

        private void BtnDistributeHorizontal_MouseDown(object sender, RoutedEventArgs e)
        {
            CurrentSheet.DistributeHorizontally();
            e.Handled = true;
        }

        private void BtnCenterVertical_Click(object sender, RoutedEventArgs e)
        {
            CurrentSheet.CenterSelectedNodesVertically();
            e.Handled = true;
        }

        private void BtnCenterHorizontal_Click(object sender, RoutedEventArgs e)
        {
            CurrentSheet.CenterSelectedNodesHorizontally();
            e.Handled = true;
        }

        private void BtnToTop_Click(object sender, RoutedEventArgs e)
        {
            CurrentSheet.AlignSelectedNodesToTop();
            e.Handled = true;
        }

        private void BtnToBottom_Click(object sender, RoutedEventArgs e)
        {
            CurrentSheet.AlignSelectedNodesToBottom();
            e.Handled = true;
        }

        private void BtnExport_Click(object sender, RoutedEventArgs e)
        {

        }

        private void Image_MouseDown(object sender, MouseButtonEventArgs e)
        {
            AddNewSheet();
        }

        private void AddNewSheet()
        {
            ScrollViewer sv = new ScrollViewer();
            sv.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
            sv.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
        }

    }
}
